package com.ayz.gateway.springbootstarter.config;

import cn.hutool.core.util.StrUtil;

import com.ayz.gateway.client.GrpcClient;
import com.ayz.gateway.client.GrpcClientConfig;
import com.ayz.gateway.common.utils.ServerAddressList;
import com.ayz.gateway.sdk.app.AppSdkClientService;
import com.ayz.gateway.sdk.app.cache.ApiDataHolder;
import com.ayz.gateway.sdk.app.config.AppSdkClientConfig;
import com.ayz.gateway.springbootstarter.application.GatewaySdkCloseListener;
import com.ayz.gateway.springbootstarter.application.GatewaySdkStartListener;
import com.ayz.gateway.springbootstarter.processor.GatewaySDKApplication;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName GatewayClientAutoConfiguration
 * @Description TODO: 网关应用程序客户端的自动装配类
 * 两个任务 网关网络通信组件SDK的注入
 *         BeanPostProcessor的注入
 * @Author 快乐的星球
 * @Date 2023/6/2 14:45
 * @Version 1.0
 **/
@Configuration
@EnableConfigurationProperties(GatewayClientProperties.class)
@Getter
@Setter
public class GatewayClientAutoConfiguration {
    @Value("${spring.application.name}")
    private String applicationName;
    @Autowired
    private GatewayClientProperties clientProperties;
    @Bean
    @ConditionalOnMissingBean
    public ApiDataHolder apiDataHolder(){
        return new ApiDataHolder();
    }
    /**
     * GRPC客户端
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public GrpcClient grpcClient(){
        //服务端的地址
        String serverAddress = clientProperties.getServerAddress();
        if(StrUtil.isBlank(serverAddress)){
            throw new IllegalArgumentException("必须指定网关注册中心的地址!参数serverAddress不能为空!");
        }
        List<String> serverAddressList = ServerAddressList.generateAddressList(serverAddress);

        GrpcClientConfig grpcConfig = clientProperties.getGrpcConfig();
        if(grpcConfig==null){
            grpcConfig=new GrpcClientConfig();
        }

        return new GrpcClient(serverAddressList,grpcConfig);
    }

    @Bean
    @ConditionalOnMissingBean
    public AppSdkClientConfig appSdkClientConfig(){
        AppSdkClientConfig clientConfig=new AppSdkClientConfig(this.applicationName,clientProperties.getGroup());
        clientConfig.setRegistryApplicationTaskTime(TimeUnit.SECONDS.toMillis((long)clientProperties.getRegistryTaskTime()));
        return clientConfig;
    }

    /**
     * 应用程序网关API注册的通信组件SDK服务
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public AppSdkClientService appSdkClientService(AppSdkClientConfig clientConfig, ApiDataHolder apiDataHolder,
                                                   GrpcClient grpcClient){
        return new AppSdkClientService(clientConfig,apiDataHolder,grpcClient);
    }

    @Bean
    @ConditionalOnMissingBean
    public GatewaySDKApplication gatewaySDKApplication(ApiDataHolder apiDataHolder){
        clientProperties.setApplicationName(getApplicationName());
        return new GatewaySDKApplication(apiDataHolder,clientProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    public GatewaySdkStartListener gatewaySdkStartListener(AppSdkClientService appSdkClientService){
        return new GatewaySdkStartListener(appSdkClientService);
    }

    @Bean
    @ConditionalOnMissingBean
    public GatewaySdkCloseListener gatewaySdkCloseListener(AppSdkClientService appSdkClientService){
        return new GatewaySdkCloseListener(appSdkClientService);
    }

}
